package com.itboye.hutouben.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * 输入限制
 */
public class EdiTextPut {


    private static String price=null;
    public static void setPricePoint(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        System.out.println("indexOf" + s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    System.out.println("substring" + s);
                    editText.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        System.out.println("subSequence" + s.subSequence(0, 1));
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                price=s+"";
                System.out.println("subSequence" + price);
                price= getPrice();

            }

        });

    }

    public static String getPrice(){
        return price;
    }
    static String temp;
    /**
     * 将时间戳转为代表"距现在多久之前"的字符串
     * @param timeStr 时间戳
     * @return
     */
    public static String getStandardDate(String timeStr) {
        try {
            long now = System.currentTimeMillis() / 1000;
            long publish = Long.parseLong(timeStr);
            long diff = now - publish;
            long months = diff / (60 * 60 * 24 * 30);
            long days = diff / (60 * 60 * 24);
            long hours = (diff - days * (60 * 60 * 24)) / (60 * 60);
            long minutes = (diff - days * (60 * 60 * 24) - hours * (60 * 60)) / 60;
            if (months > 0) {
                temp = months + "";
            } else if (days > 0) {
                temp = days + "";
            } else if (hours > 0) {
                temp = hours + "";
            } else {
                temp = minutes + "";
                temp = getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }

    public static String getTime() {
        return temp;

    }

}
